package com.example.qa_back.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qa_back.entity.QA;
import com.example.qa_back.entity.Response;
import com.example.qa_back.service.BackQAService;
import com.example.qa_back.utils.HandleExcelExport;
import com.example.qa_back.utils.HandleExcelImport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 董帅
 * @date 2021/3/6 - 18:00
 */
@RestController
@RequestMapping("/api/backQA")
public class BackQAController {
    private final BackQAService backQAService;

    public BackQAController(BackQAService backQAService) {
        this.backQAService = backQAService;
    }


    /*
    * 功能：分页获取QA对
    * */
    @PostMapping("/getQA")
    public Response getQA(@RequestBody Map<String, Integer> map) {
        Integer page = map.get("page");
        Integer num = map.get("num");
        Page<QA> qaPage = backQAService.getPage(page, num);
        return new Response(1, "获取成功", qaPage);
    }

    /*
    * 功能：根据id获取QA对
    * */
    @PostMapping("/getOneQA")
    public Response getOneQA(@RequestBody Map<String, Integer> map) {
        Integer id = map.get("id");
        QA oneQA = backQAService.getOneQA(id);
        if (oneQA == null) {
            return new Response(0, "没有此id的QA对", null);
        } else {
            return new Response(1, "获取成功", oneQA);
        }
    }

    /*
    * 功能：更新QA对
    * */
    @PostMapping("/updateQA")
    public Response updateQA(@RequestBody QA qa) {
        Integer integer = backQAService.updateQA(qa);
        if (integer == 1) {
            return new Response(1, "更新成功", null);
        } else {
            return new Response(0, "更新失败", null);
        }
    }

    /*
    * 功能：新增QA对，单个添加
    * */
    @PostMapping("/insertQA")
    public Response insertQA(@RequestBody QA qa) {
        Integer integer = backQAService.insertQA(qa);
        if (integer == 1) {
            return new Response(1, "添加成功", null);
        } else {
            return new Response(0, "添加失败", null);
        }
    }

    /*
    * 功能：新增QA对，批量添加
    * */
    @PostMapping("/insertManyQA")
    public Response insertManyQA(@RequestParam("QA") MultipartFile file) {
        List<QA> qaList = HandleExcelImport.getQAs(file);
        Integer integer = backQAService.insertManyQA(qaList);
        if (integer == 1) {
            return new Response(1, "添加成功", null);
        } else {
            return new Response(0, "添加失败", null);
        }
    }

    /*
    * 功能：删除QA对，单个删除
    * */
    @PostMapping("/deleteQA")
    public Response deleteQA(@RequestBody Map<String, Integer> map) {
        Integer id = map.get("id");
        Integer integer = backQAService.deleteQA(id);
        if (integer == 1) {
            return new Response(1, "删除成功", null);
        } else {
            return new Response(0, "删除失败", null);
        }
    }

    /*
    * 功能：删除QA对，批量删除
    * */
    @PostMapping("/deleteManyQA")
    public Response deleteManyQA(@RequestBody Map<String, Integer[]> map) {
        Integer[] ids = map.get("ids");
        Integer integer = backQAService.deleteManyQA(ids);
        if (integer == 1) {
            return new Response(1, "删除成功", null);
        } else {
            return new Response(0, "删除失败", null);
        }
    }

    /*
    * 功能：导出QA对
    * */
    @GetMapping("/exportQA")
    public Response exportQA() {
        String filePath = "F:\\q_a\\file\\QA.xlsx";  // windows
//        String filePath = "/root/q_a/QA.xlsx";  // linux
        List<String> titles = new ArrayList<>();
        titles.add("question");
        titles.add("answer");
        List<QA> allQA = backQAService.getAllQA();
        Integer integer = HandleExcelExport.exportQAs(filePath, titles, allQA, "QA对");
        if (integer == 1) {
            return new Response(1, "导出成功", null);
        } else {
            return new Response(0, "导出失败", null);
        }
    }

    /*
    * 功能：模糊搜索
    * */
    @PostMapping("/search")
    public Response search(@RequestBody Map<String, String> map) {
        String param = map.get("searchParam");
        List<QA> list = backQAService.search(param);
        if (list.size() == 0) {
            return new Response(2, "获取成功但是没有相应的搜索结果", null);
        } else {
            return new Response(1, "获取成功", list);
        }
    }
}
